package com.hmy.finance.bo.response;

import com.hmy.finance.common.utils.FinanceCalculateUtil;
import com.hmy.finance.dal.po.DeviceProfitLossDetailPo;
import com.hmy.finance.dal.po.DeviceRentalRecordPo;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Data
public class ContractDeviceDetailResponse implements Serializable {

    /**
     * 设备租赁记录ID
     */
    private Long deviceRentalRecordId;

    /**
     * 合同id
     */
    private Long contractId;

    /**
     * 合同编号
     */
    private String contractNo;

    /**
     * 设备编号
     */
    private String deviceNo;

    /**
     * 设备id
     */
    private Long deviceId;

    /**
     * 设备状态:在租；退租;
     */
    private String deviceStatementStatusDesc;

    /**
     * 设备起租时间
     */
    private LocalDateTime rentalStartTime;

    /**
     * 设备退租时间
     */
    private LocalDateTime rentalEndTime;

    /**
     * 上次对账截止时间
     */
    private LocalDateTime lastReconciliationTime;

    /**
     * 计租天数
     */
    private Double rentalDays;

    /**
     * 运费总额
     **/
    private BigDecimal shippingTotalAmount = BigDecimal.ZERO;

    /**
     * 改装费单价
     **/
    private BigDecimal modificationTotalAmount = BigDecimal.ZERO;

    /**
     * 总费用
     */
    private BigDecimal totalRevenueAmount = BigDecimal.ZERO;

    /**
     * 已对账金额
     */
    private BigDecimal totalReconciledAmount = BigDecimal.ZERO;

    public void convertToContractDeviceInfoResponse(ContractDeviceDetailResponse contractDeviceInfoResponse, DeviceRentalRecordPo deviceRentalRecordPo, Map<String, BigDecimal> deviceProfitLossMap) {
        contractDeviceInfoResponse.setDeviceRentalRecordId(deviceRentalRecordPo.getId());
        contractDeviceInfoResponse.setContractId(deviceRentalRecordPo.getContractId());
        contractDeviceInfoResponse.setContractNo(deviceRentalRecordPo.getContractExtensionInfo().getContractNo());
        contractDeviceInfoResponse.setDeviceId(deviceRentalRecordPo.getDeviceId());
        contractDeviceInfoResponse.setDeviceNo(deviceRentalRecordPo.getDeviceNo());
        contractDeviceInfoResponse.setRentalStartTime(deviceRentalRecordPo.getRentalStartTime());
        contractDeviceInfoResponse.setRentalEndTime(Objects.nonNull(deviceRentalRecordPo.getRentalEndTime()) ? deviceRentalRecordPo.getRentalEndTime() : null);
        contractDeviceInfoResponse.setDeviceStatementStatusDesc(Objects.nonNull(deviceRentalRecordPo.getRentalEndTime()) ? "退租" : "在租");
        contractDeviceInfoResponse.setLastReconciliationTime(deviceRentalRecordPo.getLastReconciliationTime());
        contractDeviceInfoResponse.setTotalRevenueAmount(Objects.isNull(deviceRentalRecordPo.getTotalRevenueAmount()) ?
                BigDecimal.ZERO : deviceRentalRecordPo.getTotalRevenueAmount());
        contractDeviceInfoResponse.setTotalReconciledAmount(Objects.isNull(deviceRentalRecordPo.getTotalReconciledAmount()) ?
                BigDecimal.ZERO : deviceRentalRecordPo.getTotalReconciledAmount());
        contractDeviceInfoResponse.setRentalDays(deviceRentalRecordPo.getTotalRentalDays());

        BigDecimal totalEntryShippingAmount = deviceProfitLossMap.get("entryShippingFee");
        BigDecimal totalExitShippingAmount = deviceProfitLossMap.get("exitShippingFee");

        contractDeviceInfoResponse.setShippingTotalAmount(totalEntryShippingAmount.add(totalExitShippingAmount));
        contractDeviceInfoResponse.setModificationTotalAmount(deviceProfitLossMap.get("modificationPayable"));
    }

}
